package com.example.petstore.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eudi.ssm.petstore.web.model.PagingList;
import com.example.petstore.persist.model.Product;
import com.example.petstore.service.ProductService;
import com.example.petstore.web.model.ErrorModel;
import com.example.petstore.web.model.SearchModel;

@Controller
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductService productService;

	@RequestMapping(value="/list")
	public String list(Model model, @ModelAttribute("searchModel") SearchModel formModel, 
			@RequestParam(value=PagingList.PAGE_INDEX_NAME, defaultValue="1") int pageIndex,
			@RequestParam(value=PagingList.PAGE_SIZE_NAME, defaultValue="10") int pageSize) {

		int id = 0;
		String name = "";
		float fromPrice = -1;
		float toPrice = -1;
		if(formModel != null) {
			id = NumberUtils.toInt(formModel.getId(), 0);
			name = formModel.getName();
			fromPrice = NumberUtils.toFloat(formModel.getFromPrice(), -1);
			toPrice = NumberUtils.toFloat(formModel.getToPrice(), -1);
		}
		model.addAttribute("searchModel", formModel);
		PagingList<Product> contentModel = this.productService.findProducts(id, name, fromPrice, toPrice, pageIndex, pageSize);
		model.addAttribute("contentModel", contentModel);

		return "product/list";
	}
	
	@RequestMapping(value="/toAddOrUpdate", method = {RequestMethod.GET})
	public String toAddOrUpdate(Model model, @RequestParam(value="id", defaultValue="0") int id) {
		
		if(id > 0) {
			Product product = this.productService.selectById(id);
			if(product != null) {
				model.addAttribute("productModel", product);
			} else {
				ErrorModel errorModel = new ErrorModel();
				errorModel.setMessage("商品不存在或已下架");
				Map<String, String> pages = new HashMap<String, String>();
				pages.put("返回上一页", "javascript:history.back();");
				errorModel.setPages(pages);
				model.addAttribute("errorModel", errorModel);
				return "comm/error";
			}
		} else {
			model.addAttribute("productModel", new Product());
		}
		return "product/addOrUpdate";
	}
	
	@RequestMapping(value="/addOrUpdate", method = {RequestMethod.POST})
	public String addOrUpdate(Model model, @Valid @ModelAttribute("productModel") Product productModel, BindingResult bindingResult) {
		
		if(bindingResult.hasErrors()) {
			return "product/addOrUpdate";
		}
		
		int id = productModel.getId();
		if(id > 0) {
			this.productService.updateProduct(productModel);
		} else {
			this.productService.addProduct(productModel);
		}
		return list(model, new SearchModel(), PagingList.DEFAULT_PAGE_INDEX, PagingList.DEFAULT_PAGE_SIZE);
	}
	
	@RequestMapping(value="/delete", method = {RequestMethod.GET})
	@ResponseBody
	public Map<String, String> delete(@RequestParam(value="id") int id) throws IOException {
		this.productService.deleteProduct(id);
		Map<String, String> map = new HashMap<String, String>(1);
		map.put("code", "1");
	    return map;
	}
}
